package com.itcm.purchase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcm.purchase.common.R;
import com.itcm.purchase.common.utils.DoubleCalcUtil;
import com.itcm.purchase.common.utils.PageUtil;
import com.itcm.purchase.entity.OutboundOil;
import com.itcm.purchase.entity.dto.PageDto;
import com.itcm.purchase.service.OutboundOilService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 出库油 前端控制器
 * </p>
 *
 * @author cm_fighting
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/purchase/outbound-oil")
@CrossOrigin
@Api(tags = "出库油")
public class OutboundOilController {

    @Autowired
    private OutboundOilService oilService;

    @PostMapping("save")
    @ApiOperation(value = "保存出库油记录")
    public R save(@RequestBody OutboundOil oil) {
        if (null != oil.getSendNum() && null != oil.getArriveNum()) {
//            oil.setPoundDiff(Math.abs(oil.getSendNum() - oil.getArriveNum()));
            oil.setPoundDiff(DoubleCalcUtil.sub(oil.getArriveNum(), oil.getSendNum()));
            if (null != oil.getPrice()) {
//                oil.setTotalAmount(oil.getPrice() * oil.getArriveNum());
                oil.setTotalAmount(DoubleCalcUtil.mul(oil.getPrice(), oil.getArriveNum()));
            }
        }
        boolean b = oilService.saveOrUpdate(oil);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("listPage")
    @ApiOperation(value = "分页查询出库油信息")
    public R listPage(@RequestBody PageDto page, String orderDate) {
        Page<OutboundOil> oilPage = PageUtil.getPageParam(page, OutboundOil.class);
        oilService.listPage(oilPage, orderDate);
        long total = oilPage.getTotal();
        List<OutboundOil> records = oilPage.getRecords();
        return R.ok().data("total", total).data("items", records);
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "delBatchIds")
    public R delBatchIds(@RequestParam String ids) {
        String[] idstr = ids.split(",");
        for (String s : idstr) {
            oilService.removeById(Long.valueOf(s));
        }
        return R.ok();
    }

    @ApiOperation(value = "获取已存在物资名称列表")
    @RequestMapping(value = "listName", method = {RequestMethod.GET, RequestMethod.POST})
    public R getExistsName() {
        List<String> list = oilService.listName();
        return R.ok().data("list", list);
    }

}

